﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WPF_SFrost
{
    public partial class FormPwdChange : Form
    {
        protected SqlConnection conn = new SqlConnection(SqlDbHelper.ConnectionString);
        protected SqlCommand cmd;
        public FormPwdChange()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtOldPwd.Text.Trim() != UserHelper.password)
            {
                MessageBox.Show("原始密码错误！");
                txtOldPwd.Focus();
                return;
            }
            if (txtNewPwd.Text.Trim() != txtNewPwdAgain.Text.Trim())
            {
                MessageBox.Show("两次输入的新密码不一致！");
                txtNewPwd.Focus();
                return;
            }
            //string sql = string.Format("update [user] set password = '{0}' where username = '{1}'",
              //  txtNewPwd.Text.Trim(), UserHelper.username);
            string sql = "update [user] set password=@password where username=@username";
            conn.Open();
            cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("@password", txtNewPwd.Text.Trim());
            cmd.Parameters.AddWithValue("@username", UserHelper.username);
            int n = cmd.ExecuteNonQuery();
            if (n ==1)
            {
                MessageBox.Show("修改密码成功，下次登录请输入新密码。");
                UserHelper.password = txtNewPwd.Text.Trim();
            }
            else
            {
                MessageBox.Show("密码修改失败！");
            }
            conn.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormPwdChange_Load(object sender, EventArgs e)
        {
            txtUserName.Text = UserHelper.username;
        }
    }
}
